import java.util.ArrayList;


public class Ex7_1 {

	/**
	 * PLEASANTLY
	   -ME--AN-LY
	   https://class.coursera.org/bioinformatics-001/forum/thread?thread_id=768
	 */
	public Ex7_1() {
		resultV = "";
		resultW = "";
		distance = 0;
	}

	public String resultV, resultW;
	public Integer distance;

	public static void main(String[] args) {
		Ex7_1 exer = new Ex7_1();		
		ArrayList<String> inputs = Utils.readLines("C:\\Users\\josemfer\\Downloads\\dataset_77_3.txt");
		String v = inputs.get(0);
		String w = inputs.get(1);
		ArrayList<Integer[][]> results = exer.getBacktrack(v, w);
		Integer[][] backt = results.get(0);
		Integer[][] scores = results.get(1);
		Integer finalScore = scores[v.length()][w.length()];
		exer.getOutputLCS(backt,v,w,v.length(),w.length());
		Utils.writeFile( finalScore + "\r\n" 
				+ exer.resultV + "\r\n" 
				+ exer.resultW, 
				"C:\\Users\\josemfer\\Downloads\\out_7_1.txt");
		System.out.println(finalScore);
		System.out.println(exer.resultV);
		System.out.println(exer.resultW);
		System.out.println(exer.distance);
		

	}


	private void getOutputLCS(Integer[][] backtrack, 
			String v, String w, Integer i, Integer j) {
		//System.out.println("i=" + i + "   j=" + j);
		if (i == 0 && j == 0) {
			return;
		}
		if (backtrack[i][j] == 0) { // '0' = down
			getOutputLCS(backtrack,v,w,i-1,j);			
			resultV += v.charAt(i-1);
			resultW += "-";
			distance++;

		} else if (backtrack[i][j] == 1) {   // '1' = right 
			getOutputLCS(backtrack,v,w,i,j-1);
			resultV += "-";
			resultW += w.charAt(j-1);
			distance++;

		} else if (backtrack[i][j] == 2) {
			getOutputLCS(backtrack,v,w,i-1,j-1);   //  '2' = diagonal
			resultV += v.charAt(i-1);
			resultW += w.charAt(j-1);		
			if (v.charAt(i-1) != w.charAt(j-1)) {
				distance++;
			}
		}		
	}	

	/*
	 *   '0' = down   '1' = right   '2' = diagonal
	 */
	private ArrayList<Integer[][]> getBacktrack(String v, String w) {	
		ArrayList<Integer[][]> results = null;			
		Integer sigma = 0;		
		Integer n = v.length();
		Integer m = w.length();
		Integer s[][] = new Integer[n+1][m+1];
		Integer back[][] = new Integer[n+1][m+1];		
		for (int i=0;i<=n;i++) {
			s[i][0] = sigma*i;
			//back[i][0] = 0; // down
		}
		for (int j=0;j<=m;j++) {
			s[0][j] = sigma*j;
			//back[0][j] = 1; // right
		}
		for (int i=1;i<=n;i++) {
			for (int j=1;j<=m;j++) {
				// calculate down and right
				Integer down = s[i-1][j] + sigma;
				Integer right = s[i][j-1] + sigma;
				Integer diag = s[i-1][j-1] +1 ;
				// calculate diag				
				char vi = v.charAt(i-1);
				char wj = w.charAt(j-1);
				
				//System.out.println(vi + " " + wj);
				
				if (vi == wj) {
					diag = diag + 1;
				}				
				//save score and backtrack matrix
				if ((down == right) && (right == diag)) {
					s[i][j]  = 1;
					back[i][j] = 2;					
				} else {				
					s[i][j] = Utils.max(new Integer[]{down,right,diag});			 
					back[i][j] = Utils.maxIndex(new Integer[]{down,right,diag});
				}	
			}				
		}
		results = new ArrayList<Integer[][]>();		
		results.add(back);		
		results.add(s);		
		return results;
	}

}
